package com.yanghx.user.server.controller;

import cn.hutool.core.util.StrUtil;
import com.yanghx.user.server.constant.CookieConstant;
import com.yanghx.user.server.constant.RedisConstant;
import com.yanghx.user.server.entity.UserInfo;
import com.yanghx.user.server.enums.ResultEnum;
import com.yanghx.user.server.enums.RoleEnum;
import com.yanghx.user.server.service.UserService;
import com.yanghx.user.server.util.CookieUtil;
import com.yanghx.user.server.vo.ResultVO;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @author yangHX
 * createTime  2019/9/14 21:07
 */
@RequestMapping
@RestController("/login")
public class LoginController {

    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Resource
    private UserService userService;

    @GetMapping(value = "/buyer")
    public ResultVO buyer(@RequestParam("openid") String openid, HttpServletResponse response) {

        //1. openid和数据库里的数据是否匹配
        UserInfo userInfo = userService.findByOpenid(openid);
        if (userInfo == null) {
            return ResultVO.error(ResultEnum.LOGIN_FAIL);
        }
        //2. 判断角色
        if (!RoleEnum.BUYER.getCode().equals(userInfo.getRole())) {
            return ResultVO.error(ResultEnum.ROLE_ERROR);
        }
        //3. cookie里设置openid=abc
        CookieUtil.set(response, CookieConstant.OPENID, openid, CookieConstant.expire);

        return ResultVO.success();
    }


    @GetMapping(value = "/seller")
    public ResultVO seller(@RequestParam("openid") String openid, HttpServletRequest request, HttpServletResponse response) {

        //判断是否已登录
        Cookie cookie = CookieUtil.get(request, CookieConstant.TOKEN);
        if (cookie != null &&
                !StringUtils.isEmpty(stringRedisTemplate.opsForValue().get(StrUtil.format(RedisConstant.TOKEN_TEMPLATE, cookie.getValue())))) {
            return ResultVO.success();
        }
        //1. openid和数据库里的数据是否匹配
        UserInfo userInfo = userService.findByOpenid(openid);
        if (userInfo == null) {
            return ResultVO.error(ResultEnum.LOGIN_FAIL);
        }
        //2. 判断角色
        if (!RoleEnum.SELLER.getCode().equals(userInfo.getRole())) {
            return ResultVO.error(ResultEnum.ROLE_ERROR);
        }
        //3. redis设置key=UUID, value=xyz
        String token = UUID.randomUUID().toString();
        Integer expire = CookieConstant.expire;
        stringRedisTemplate.opsForValue().set(StrUtil.format(RedisConstant.TOKEN_TEMPLATE, token)
                , openid, expire, TimeUnit.SECONDS);
        //4. cookie里设置token=UUID
        CookieUtil.set(response, CookieConstant.TOKEN, token, CookieConstant.expire);
        return ResultVO.success();
    }

}
